using UnityEngine;
using System.Collections;

public class playerLixo : MonoBehaviour
{
	int id = 7;
    private Vector2 screen;

    void Start()
    {
        this.screen.y = GameObject.Find("Main Camera").GetComponent<Camera>().orthographicSize;
        this.screen.x = this.screen.y * GameObject.Find("Main Camera").GetComponent<Camera>().aspect;

		this.gameObject.AddComponent("MeshRenderer");
        this.gameObject.AddComponent("MeshFilter");
		this.gameObject.AddComponent("BoxCollider");
		this.gameObject.GetComponent<BoxCollider>().size = new Vector3(this.screen.x * 0.2f, 1, 0);
        this.gameObject.GetComponent<BoxCollider>().center = new Vector3(0, this.screen.y * -0.1f, 0);
        this.gameObject.AddComponent("Rigidbody");
        this.gameObject.GetComponent<Rigidbody>().useGravity = false;

        this.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
		this.gameObject.tag = "playerLixo";

        Mesh mesh = Quad.Create(this.screen.x * 0.1f, this.screen.y * 0.2f);
		
        this.gameObject.GetComponent<MeshFilter>().mesh = mesh;
        this.gameObject.GetComponent<MeshRenderer>().material = AtlasManager.globalMaterial;

        this.gameObject.GetComponent<MeshFilter>().mesh.uv = new Vector2[]
        {
           new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMax),
           new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMax),
           new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMin),
           new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMin)
        };

        this.transform.position = new Vector3(this.screen.x * -0.5f, this.screen.y * -0.6f, -1);   
	}
	
	void Update () 
	{
		this.gameObject.GetComponent<MeshFilter>().mesh.uv = new Vector2[]
        {
           new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMax),
           new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMax),
           new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMin),
           new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMin)
        };
		
		//Movimentaçao
		if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
		{
			transform.Translate(0.1f, 0, 0);
			id = 7;
		}
		if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
		{
			transform.Translate(-0.1f, 0, 0);
			id = 9;
		}
		
		//Colisao
        //if(transform.position.x >= 4.7f)
        //{
        //    this.transform.position = new Vector3(4.7f, transform.position.y, transform.position.z);
        //}
		
        //if(transform.position.x <= -4.5f)
        //{
        //    this.transform.position = new Vector3(-4.5f, transform.position.y, transform.position.z);
        //}		
	}

    void OnCollisionEnter(Collision c)
    {
        if (c.gameObject.tag.Equals("itemLixo"))
        {
            Destroy(c.gameObject);
            gameLixo.score--;
        }

        if (c.gameObject.tag.Equals("criancinhaLixo"))
        {
            Destroy(c.gameObject);
            gameLixo.life--;
        }
    }
}
	